package com.isman.placeslist.ws;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.isman.placeslist.apiobject.SingUp;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;

public class ServiceProvider {
	private Context context;
	private AsyncRunner mRunner;

	public ServiceProvider(Context context) {
		this.context = context;
	}

	public boolean Cancel(boolean mayInterruptIfRunning) {
		if (mRunner != null) {
			return mRunner.cancel(mayInterruptIfRunning);
		}
		return false;
	}

	// Starter User
	public void CheckEmail(String email, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/check/" + email, headler);
		mRunner.execute();
	}

	public void ChechFacebook(String email, String fbId,
			AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/check/" + email + "/" + fbId, headler);
		mRunner.execute();
	}

	public void SingIn(String email, String password, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/singin/" + email + "/" + password, headler);
		mRunner.execute();
	}

	public void SingUpWithEmail(SingUp singup, AsyncRunnerHeadler headler) {
		Bundle bundle = new Bundle();
		bundle.putString("firstname", singup.getFirstname());
		bundle.putString("lastname", singup.getLastname());
		bundle.putString("email", singup.getEmail());
		bundle.putString("password", singup.getPassword());
		bundle.putString("telephone", singup.getTelephone());
		bundle.putString("birthday", singup.getBirthday());
		bundle.putString("gender", singup.getGender());

		mRunner = new AsyncRunner(context);
		mRunner.exe("/singup/email", bundle, AsyncRunner.POST, headler);
		mRunner.execute();
	}

	public void SingUpWithFacebook(SingUp singup, AsyncRunnerHeadler headler) {
		Bundle bundle = new Bundle();
		bundle.putString("firstname", singup.getFirstname());
		bundle.putString("lastname", singup.getLastname());
		bundle.putString("email", singup.getEmail());
		bundle.putString("password", singup.getPassword());
		bundle.putString("telephone", singup.getTelephone());
		bundle.putString("birthday", singup.getBirthday());
		bundle.putString("gender", singup.getGender());

		mRunner = new AsyncRunner(context);
		mRunner.exe("/singup/" + singup.getId(), bundle, AsyncRunner.POST,
				headler);
		mRunner.execute();
	}

	// Status
	public void LoadFeed(long mUserId, AsyncRunnerHeadler mHeadler) {
		this.LoadFeed(mUserId, 0, 8, mHeadler);
	}

	public void LoadFeed(long mUserId, int start, int end,
			AsyncRunnerHeadler mHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/feed/" + mUserId + "/" + start + "/" + end, mHeadler);
		mRunner.execute();
	}

	// Comment
	public void LoadComment(long mParentId, AsyncRunnerHeadler mHeadle) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/comments/" + mParentId, mHeadle);
		mRunner.execute();
	}

	// Create status and comment
	public void CreateStatus(String message, String toId, String currentId,
			String imagePath, int policy, ProgressDialog progressDialog,
			AsyncRunnerHeadler headler) {
		Bundle bundle = new Bundle();
		bundle.putString("message", message);
		bundle.putString("toId", String.valueOf(toId));
		bundle.putString("policy", String.valueOf(policy));
		bundle.putString("currentId", String.valueOf(currentId));

		if (!imagePath.equals("")) {
			bundle.putString("image", imagePath);
		}

		mRunner = new AsyncRunner(context);
		mRunner.exe("/status/", bundle, AsyncRunner.POST, progressDialog,
				headler);
		mRunner.execute();
	}

	public void CreateComment(long id, String message,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		Bundle bundle = new Bundle();
		bundle.putString("parentId", String.valueOf(id));
		bundle.putString("message", message);

		mRunner = new AsyncRunner(context);
		mRunner.exe("/comment/", bundle, AsyncRunner.POST, asyncRunnerHeadler);
		mRunner.execute();
	}

	// Like and unlike status.
	public void StatusLike(long statusId, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/status/" + statusId + "/like", new Bundle(),
				AsyncRunner.POST, headler);
		mRunner.execute();
	}

	public void StatusUnlike(long statusId, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/status/" + statusId + "/like", new Bundle(),
				AsyncRunner.DELETE, headler);
		mRunner.execute();
	}

	// Like and unlike comment.
	public void CommentLike(long commentId, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/comment/" + commentId + "/like", new Bundle(),
				AsyncRunner.POST, headler);
		mRunner.execute();
	}

	public void CommentUnlike(long commentId, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/comment/" + commentId + "/like", new Bundle(),
				AsyncRunner.DELETE, headler);
		mRunner.execute();
	}

	// Delete status and comment.
	public void DeleteStatus(long statusId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/status/" + statusId, new Bundle(), AsyncRunner.DELETE,
				asyncRunnerHeadler);
		mRunner.execute();
	}

	public void DeleteComment(long commentId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/comment/" + commentId, new Bundle(), AsyncRunner.DELETE,
				asyncRunnerHeadler);
		mRunner.execute();
	}

	// Create status report and comment report
	public void CreateStatusReport() {
	}

	public void CreateCommentReport() {
	}

	public void syncPeople(Bundle bundle, AsyncRunnerHeadler headler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people", bundle, AsyncRunner.POST, headler);
		mRunner.execute();
	}

	public void loadPeople(AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people", asyncRunnerHeadler);
		mRunner.execute();
	}

	// Friend
	public void getFriendDetail(long userId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + userId, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void cancelRequestFriend(long friendId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId + "/request", new Bundle(),
				AsyncRunner.DELETE, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void addRequestFriend(long friendId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId + "/request", new Bundle(),
				AsyncRunner.POST, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void acceptFriend(long friendId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId + "/accept", new Bundle(),
				AsyncRunner.POST, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void unAcceptFriend(long friendId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId + "/accept", new Bundle(),
				AsyncRunner.DELETE, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void unFriend(long friendId, AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId, new Bundle(), AsyncRunner.POST,
				asyncRunnerHeadler);
		mRunner.execute();
	}

	public void blockFriend(long friendId, AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/people/" + friendId, new Bundle(), AsyncRunner.PUT,
				asyncRunnerHeadler);
		mRunner.execute();
	}

	public void GetPlaceCategories(AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/place/categories", asyncRunnerHeadler);
		mRunner.execute();
	}

	public void CreatePlace(long categoriesId, Bitmap imagePlace,
			String namePlace, String description, String locality,
			String subLocality, String countryName, String countryCode,
			String postalCode, double latitude, double longitude,
			AsyncRunnerHeadler asyncRunnerHeadler) {

		File file = null;
		try {
			if (imagePlace != null) {
				file = File.createTempFile("temp", ".jpg",
						context.getCacheDir());
				imagePlace.compress(CompressFormat.JPEG, 100,
						new FileOutputStream(file));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		Bundle bundle = new Bundle();
		bundle.putString("categoriesId", String.valueOf(categoriesId));
		bundle.putString("namePlace", namePlace);
		bundle.putString("description", description);
		bundle.putString("locality", locality);
		bundle.putString("subLocality", subLocality);
		bundle.putString("countryName", countryName);
		bundle.putString("countryCode", countryCode);
		bundle.putString("postalCode", postalCode);
		bundle.putString("latitude", String.valueOf(latitude));
		bundle.putString("longitude", String.valueOf(longitude));
		if (file != null) {
			bundle.putString("image-temp", file.getAbsolutePath());
		}

		mRunner = new AsyncRunner(context);
		mRunner.exe("/place/", bundle, AsyncRunner.POST, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void GetLocation(long mLocationId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/place/" + mLocationId + "/detail", asyncRunnerHeadler);
		mRunner.execute();
	}

	public void UnlikeLocation(long mLocationId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/place/" + mLocationId + "/like", new Bundle(),
				AsyncRunner.POST, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void LikeLocation(long mLocationId,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe("/place/" + mLocationId + "/like", new Bundle(),
				AsyncRunner.DELETE, asyncRunnerHeadler);
		mRunner.execute();
	}

	public void SearchPlace(double d, double e, String str,
			AsyncRunnerHeadler asyncRunnerHeadler) {
		mRunner = new AsyncRunner(context);
		mRunner.exe(
				"/place/search/" + String.valueOf(d) + "/"
						+ String.valueOf(e) + "/" + str, asyncRunnerHeadler);
		mRunner.execute();
	}
}

// public void CheckIn(long locationId, String message, int policy,
// AsyncRunnerHeadler headler){
// Bundle bundle = new Bundle();
// bundle.putString("message", message);
// bundle.putString("policy", String.valueOf(policy));
//
// AsyncRunner runner = new AsyncRunner(context);
// runner.exe("/location/"+String.valueOf(locationId)+"/like", bundle,
// AsyncRunner.POST, headler);
// runner.execute();
// }
//
// public void loadLocationSuggest(String text, AsyncRunnerHeadler headler) {
// AsyncRunner runner = new AsyncRunner(context);
// runner.exe("/location/suggest/"+URLEncoder.encode(text), headler);
// runner.execute();
// }
// public void loadSearchSuggest(String text, AsyncRunnerHeadler headler) {
// AsyncRunner runner = new AsyncRunner(context);
// runner.exe("/location/searchsuggest/"+URLEncoder.encode(text), headler);
// runner.execute();
// }
//
// public void searchLocation(String text, int start, int end,
// AsyncRunnerHeadler headler) {
// AsyncRunner runner = new AsyncRunner(context);
// runner.exe("/search/"+URLEncoder.encode(text)+"/0/"+start+"/"+end, headler);
// runner.execute();
// }